var clientId = '11806248479-26larg86v6n69mt7l4ofj0ed86vd581f.apps.googleusercontent.com';
var apiKey = get_google_tasks_apikey();
var scopes = 'https://www.googleapis.com/auth/tasks';
var timer;

run_google_all_tasks();

function get_google_tasks_apikey() {
    if (document.domain != "localhost") {
        apiKey = 'AIzaSyAa9POqjirDVqV5hTUTiKUFfMfGoEhWrJY';
        console.log("online");
    } else {
        apiKey = 'AIzaSyD2BiYFZuW2hyuQdH6ykQ6x4nse7Czspx4';
        console.log("local");
    }
}

function run_google_all_tasks() {
    gapi.client.setApiKey(apiKey);
    window.setTimeout(checkAuth, 1);
}

function checkAuth() {
    gapi.auth.authorize({
        client_id: clientId,
        scope: scopes,
        immediate: true
    }, handleAuthResult);
}

function handleAuthResult(authResult) {
    var authorizeButton = document.getElementById('authorize-button');
    if (authResult && !authResult.error) {
        authorizeButton.style.visibility = 'hidden';
        initApiCall();
    } else {
        authorizeButton.style.visibility = '';
        authorizeButton.onclick = handleAuthClick;
    }
}

function handleAuthClick(event) {
    gapi.auth.authorize({
        client_id: clientId,
        scope: scopes,
        immediate: false
    }, handleAuthResult);
    return false;
}
